# -*- |coding: utf-8 -*-
import json

import pymysql


class Question():

    def __init__(self):
        self.conn = pymysql.connect(host="localhost", user="root", password="123456", db="yikaobang", port=3306,
                               charset='utf8')
        self.client = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

    def show(self):
        sql = 'select * from QUESTION_INFO_BEAN'
        self.client.execute(sql)
        data = self.client.fetchall()
        for i in data:
            question_id = i['QUESTION_ID']
            sql = 'select * from QUESTION_OPTION_BEAN where QUESTION_ID={}'.format(question_id)
            self.client.execute(sql)
            choose = self.client.fetchall()
            try:
                i['choose'] = json.dumps(choose, ensure_ascii=False)
            except Exception:
                continue
            with open('./dump.sql', 'a') as f:
                values = str(i.values()).replace('dict_values([', '(').replace('])', ')')
                sql = 'INSERT INTO QUESTION_INFO_BEAN VALUES' + values + ';' + '\n'
                f.write(sql)
                print('Already save: {}'.format(sql))

q = Question()
q.show()